// pages/route/daily/timetable/timetable.js
const app = getApp();
const service = require('../../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    navIdx: 0,
    // 选择的时间
    date: null,
    // 选择的完整时间
    chooseTime: null,
    // 星期几
    weekday: null,
    // 当前时间
    nowDate: null,
    // 路线列表
    routeList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let date = new Date()
    this.setData({
      weekday: '星期' + app.utils.parseTime(date, '{a}'),
      date: app.utils.parseTime(date, '{m}-{d}'),
      nowDate: app.utils.parseTime(new Date(), '{y}-{m}-{d}'),
      chooseTime: app.utils.parseTime(new Date(), '{y}-{m}-{d}')
    });
    this.loadData(app.utils.parseTime(new Date(), '{y}-{m}-{d}'));
  },

  // 加载数据
  loadData(date){
    let that = this;
    let params = {
      time: date,
      pageSize: 20,
      page: this.data.page
    }
    service.getConnect(service.api.getVerifierClassesList, params).then(function (res) {
      wx.hideLoading();
      that.setData({
        routeList: that.data.routeList.concat(res.data)
      })
    });
  },

  // 前一天
  bindReduceDay(){
    let date = app.utils.parseTime(new Date(this.data.nowDate).getTime() - 86400000, '{y}-{m}-{d}');
    this.setData({
      chooseTime: date,
      navIdx: 1,
      page: 1,
      routeList: []
    })
    this.loadData(date);
  },

  // 当前日期
  bindNowDay() {
    let date = app.utils.parseTime(new Date(this.data.nowDate).getTime(), '{y}-{m}-{d}');
    this.setData({
      chooseTime: date,
      navIdx: 0,
      page: 1,
      routeList: []
    })
    this.loadData(date);
  },

  // 后一天
  bindPlusDay(){
    let date = app.utils.parseTime(new Date(this.data.nowDate).getTime() + 86400000, '{y}-{m}-{d}');
    this.setData({
      chooseTime: date,
      navIdx: 2,
      page: 1,
      routeList: []
    })
    this.loadData(date);
  },

  // 路线班次详情
  bindToRouteDetails(e) {
    wx.navigateTo({
      url: '/pages/verifier/routeDetail/index?id=' + e.currentTarget.dataset.id + '&chooseTime=' + this.data.chooseTime+' '+e.currentTarget.dataset.shift
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.pageOnloadInit(this, app);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      routeList: []
    })
    this.loadData();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.page++;
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
